package org.springblade.common.config;


import org.springblade.common.filter.PreviewFilter;
import org.springblade.core.secure.registry.SecureRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Blade配置
 *
 * @author Chill
 */
@Configuration
public class BladeConfiguration implements WebMvcConfigurer {

    /**
     * global public resource exclusion config
     *
     * @return
     */
    @Bean
    public SecureRegistry secureRegistry() {
        SecureRegistry secureRegistry = new SecureRegistry();
        secureRegistry.setEnabled(true);
        //过滤不检测token的接口 未登录也可以
        secureRegistry.excludePathPatterns("/blade-auth/**");
        secureRegistry.excludePathPatterns("/blade-log/**");
        secureRegistry.excludePathPatterns("/blade-system/menu/routes");
        secureRegistry.excludePathPatterns("/blade-system/dict/**");
        secureRegistry.excludePathPatterns("/blade-develop/**");
        secureRegistry.excludePathPatterns("/blade-system/menu/auth-routes");
        secureRegistry.excludePathPatterns("/blade-system/menu/top-menu");
        secureRegistry.excludePathPatterns("/blade-system/tenant/info");
        secureRegistry.excludePathPatterns("/blade-system/region/**");
        secureRegistry.excludePathPatterns("/doc.html");
        secureRegistry.excludePathPatterns("/js/**");
        secureRegistry.excludePathPatterns("/webjars/**");
        secureRegistry.excludePathPatterns("/swagger-resources/**");
        secureRegistry.excludePathPatterns("/druid/**");
        secureRegistry.excludePathPatterns("/password/**");

///////////////TODO 第三方不授权回调通知 纳入白名单校验
//////////////////////自定义接口开发
        //汽配城
        secureRegistry.excludePathPatterns("/blade-app/xfc/**");
        secureRegistry.excludePathPatterns("/blade-app/advertise/**");
        secureRegistry.excludePathPatterns("/blade-app/helpstar/**");
        ///FIXME 微信支付回调
        secureRegistry.excludePathPatterns("/blade-app/wxNotify/**");
        secureRegistry.excludePathPatterns("/blade-app/wxNotify/payOrderNotify");


        //文章的
        secureRegistry.excludePathPatterns("/blade-app/article/pageSearch");
        secureRegistry.excludePathPatterns("/blade-app/article/pageSearchHot");
        secureRegistry.excludePathPatterns("/blade-app/article/detail");
        secureRegistry.excludePathPatterns("/blade-app/article/page");
        secureRegistry.excludePathPatterns("/blade-app/article/getArticleInfo");
        secureRegistry.excludePathPatterns("/blade-app/searchtag/**");
        secureRegistry.excludePathPatterns("/blade-app/video/**");
        secureRegistry.excludePathPatterns("/blade-app/article/getNewOneArticle");
        secureRegistry.excludePathPatterns("/blade-app/article/getMoreArticle");
        secureRegistry.excludePathPatterns("/blade-app/fquser/**");

        //文章类型
        secureRegistry.excludePathPatterns("/blade-app/articletype/**");
        secureRegistry.excludePathPatterns("/blade-system/dict-biz/**");

        secureRegistry.excludePathPatterns("/share/**");
        ////demand
        secureRegistry.excludePathPatterns("/blade-app/demandtype/**");
        secureRegistry.excludePathPatterns("/blade-app/demand/getDemandOne");
        secureRegistry.excludePathPatterns("/blade-app/demand/getDemandMore");
        secureRegistry.excludePathPatterns("/blade-app/helpstar/**");

        ///upload and huixian
        secureRegistry.excludePathPatterns("/blade-app/filemgr/**");
        secureRegistry.excludePathPatterns("/upload/**");


        ////visual daping
        secureRegistry.excludePathPatterns("/blade-app/blade-visual/**");
        secureRegistry.excludePathPatterns("/blade-visual/**");
        secureRegistry.excludePathPatterns("/blade-system/dept/getDeptPageByAreaInfo");
        ///huodong
        secureRegistry.excludePathPatterns("/blade-app/huodong/page");
        secureRegistry.excludePathPatterns("/blade-app/huodong/hdListByUserGetOne");
        secureRegistry.excludePathPatterns("/blade-app/huodong/hdListByMore");
        secureRegistry.excludePathPatterns("/blade-app/huodong/hdListByUser");
        secureRegistry.excludePathPatterns("/blade-app/huodong/page");
        secureRegistry.excludePathPatterns("/blade-app/huodong/DPHuodongList");
        secureRegistry.excludePathPatterns("/blade-app/huodong/getHuodongPageByAreaInfo");
        secureRegistry.excludePathPatterns("/blade-app/huodong/getHuodongJwdByAreaInfo");
        secureRegistry.excludePathPatterns("/blade-app/huodong/getHdAndUserBarByMonth");
        secureRegistry.excludePathPatterns("/blade-app/huodong/getHuodongListPart");
        secureRegistry.excludePathPatterns("/blade-app/huodong/getHuodongPageAllBySearch");
        secureRegistry.excludePathPatterns("/blade-app/huodong/getHuodongInfo");
        secureRegistry.excludePathPatterns("/blade-app/huodong/exportHuodong");

        secureRegistry.excludePathPatterns("/blade-app/teams/listTeamsByArea");
        secureRegistry.excludePathPatterns("/blade-app/teams/getTeamsJwdByAreaInfo");
        secureRegistry.excludePathPatterns("/blade-app/teams/getTeamsListByAreaInfo");
        secureRegistry.excludePathPatterns("/blade-app/teams/getTeamsListByAreaInfo");
        secureRegistry.excludePathPatterns("/blade-app/teams/getDpTeamsListAll");
        secureRegistry.excludePathPatterns("/blade-app/teams/getDpTeamsListPart");
        secureRegistry.excludePathPatterns("/blade-app/teams/getTeamsPageAllBySearch");
        secureRegistry.excludePathPatterns("/blade-app/teams/getTeamsInfo");
        secureRegistry.excludePathPatterns("/blade-app/teams/exportTeamsList");

        return secureRegistry;
    }

    @Bean
    @ConditionalOnProperty(value = "blade.preview.enabled", havingValue = "true")
    public PreviewFilter previewFilter() {
        return new PreviewFilter();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //FIXME 1.禁止全局跨域和全路径跨域
        //FIXME 2.跨域白名单配置
        registry.addMapping("/**")
                .allowedOrigins("*")
//				.allowedHeaders("*")
                .allowedHeaders("", "Access-Control-Allow-Origin")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/**").addResourceLocations("file:/opt/");
        //..others no load
    }

}
